package servlet;

import dao.CarouselDAO;
import pojo.Carousel;
import utils.ApiResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/carousel")
public class CarouselServlet extends BaseServlet {

    // 获取所有轮播图
    public void getAllCarousels(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Carousel> carousels = CarouselDAO.getAllCarousels();
            sendJsonResponse(resp, new ApiResult(200, "获取所有轮播图成功", carousels));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "获取所有轮播图失败", e.getMessage()));
        }
    }

    // 根据ID获取轮播图
    public void getCarouselById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int carouselId = Integer.parseInt(req.getParameter("carouselId"));
        try {
            Carousel carousel = CarouselDAO.getCarouselById(carouselId);
            if (carousel != null) {
                sendJsonResponse(resp, new ApiResult(200, "获取轮播图成功", carousel));
            } else {
                sendJsonResponse(resp, new ApiResult(404, "轮播图不存在", null)); // 404 Not Found
            }
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "获取轮播图失败", e.getMessage()));
        }
    }

    // 新增轮播图
    public void insertCarousel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String title = req.getParameter("title");
        String description = req.getParameter("description");
        String imageUrl = req.getParameter("imageUrl");

        Carousel carousel = new Carousel();
        carousel.setTitle(title);
        carousel.setDescription(description);
        carousel.setImageUrl(imageUrl);

        try {
            CarouselDAO.insertCarousel(carousel);
            sendJsonResponse(resp, new ApiResult(200, "新增轮播图成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "新增轮播图失败", e.getMessage()));
        }
    }

    // 更新轮播图
    public void updateCarousel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int carouselId = Integer.parseInt(req.getParameter("carouselId"));
        String title = req.getParameter("title");
        String description = req.getParameter("description");
        String imageUrl = req.getParameter("imageUrl");

        Carousel carousel = new Carousel();
        carousel.setCarouselId(carouselId);
        carousel.setTitle(title);
        carousel.setDescription(description);
        carousel.setImageUrl(imageUrl);

        try {
            CarouselDAO.updateCarousel(carousel);
            sendJsonResponse(resp, new ApiResult(200, "更新轮播图成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "更新轮播图失败", e.getMessage()));
        }
    }

    // 删除轮播图
    public void deleteCarousel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int carouselId = Integer.parseInt(req.getParameter("carouselId"));
        try {
            CarouselDAO.deleteCarousel(carouselId);
            sendJsonResponse(resp, new ApiResult(200, "删除轮播图成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "删除轮播图失败", e.getMessage()));
        }
    }
}
